app.controller('loanRepay', function($element, $scope, $state, rest, user, uploadConf, tipService, $ocLazyLoad, ngDialog, uiLoad, $localStorage, $timeout) {
  var data = angular.copy($scope.ngDialogData, {});

  $scope.params = {};
  $scope.params2 = {};
  $scope.params3 = {};
  $scope.params2.childOrderId = data.repayOrderId;
  $scope.repayAmount = parseFloat(data.repayAmount);
  $scope.isSetPassword = user.get('isTransPasswordSet');
  $scope.user = {
    phone: user.get('phone'),
    PasswordType: 1
  }
  rest.post({
    methodId: 'J018',
    data: {
      'accountOpenId': user.get('openId')
    },
    // data: {'accountOpenId': '1dca25eabc753b7087ef659a5d7c5c98'},
    success: function(res) {
      console.log(res);
      $scope.totalAmount = parseFloat(res.data.realAmount);
      $scope.totalAmountLeft = res.data.realAmount;

      if ($scope.repayAmount > $scope.totalAmountLeft) {
        $scope.enoughMoney = false;
      } else {
        $scope.enoughMoney = true;
      }
      $scope.$apply();
    },
    error: function() {

    }
  })
  $scope.goRecharge = function() {
    $scope.$emit('msg2', 'Hello Angular!');
    $state.go('app.fundManage.fundList');
    $scope.confirm({});
  }

  //点击确定按钮
  $scope.modifySure1 = function() {
    // if ($scope.params.contractPics.length<1) {
    // tipService.show("body", {
    // message: "合同图片必传",
    // notice:"danger"
    // }, 3);
    // return false;
    // }

    $scope.params.RechargeAmount = $scope.amount;
    $scope.params.OpenId = user.get('openId'),
      //$scope.params.OpenId = '1dca25eabc753b7087ef659a5d7c5c98';
      $scope.params.Phone = user.get('phone'),

      $scope.params.LandlordId = user.get('landlordId');
    $scope.params.TransPassword = $scope.transPassword;
    rest.post({
        methodId: '5020',
        data: $scope.params,
        success: function(res) {
          if (res.code == 0) {
            rest.post({
              methodId: '4037',
              data: $scope.params2,
              success: function(res1) {
                if (res1.code == 0) {
                  $scope.params3.projectNo = res1.res.data.sceneAndParams.projectNo;
                  $scope.params3.projectOrderNo = res1.res.data.projectOrderNo;
                  $scope.params3.description = "分期还款";
                  $scope.params3.accountOpenId = user.get('openId');
                  $scope.params3.fitmentMoney = res1.res.data.fitmentMoney;
                  rest.post({
                    methodId: 'J022',
                    data: $scope.params3,
                    success: function(res2) {
                      console.log(res2);
                      tipService.show("body", {
                        message: res2.res.msg
                      }, 3);
                      $timeout(function() {
                        // location.reload();
                      }, 2000);
                    },
                    error: function() {}
                  })

                } else {
                  tipService.show("body", {
                    message: res1.res.msg,
                    notice: "danger"
                  }, 3);
                  $timeout(function() {
                    // location.reload();
                  }, 2000);
                }
              },
              error: function() {}
            })
          } else if (res.code == 410) {
            tipService.show("body", {
              message: res.res.data.msg,
              notice: "danger"
            }, 3);
          }
        },
        error: function() {}
      })
      // $scope.confirm({});
  }
  $scope.modifySure = function() {
    ngDialog.openConfirm({
      template: 'js/module/fund/charge.html',
      resolve: {
        load: function() {
          return uiLoad.load(['js/module/fund/charge.js'])
        }
      },
      data: {

      },
      scope: $scope,
      width: 600,
      // height: 700
    }).then(function(data) {

    }, function(reason) {

    });
  }

  $scope.$watch('newPassword', function(newValue, oldValue) {
    $scope.user.newPassword = newValue;
  })
  $scope.$watch('comfirmedPassword', function(newValue, oldValue) {
    $scope.user.comfirmedPassword = newValue;
  })
  $scope.saveData = function() {
    if ($scope.newPassword != $scope.comfirmedPassword) {
      tipService.show("body", {
        message: "两次密码输入不一致",
        notice: "danger"
      }, 3);
      return;
    }
    if ($scope.newPassword.length != 6) {
      tipService.show("body", {
        message: "需要输入6位资金密码",
        notice: "danger"
      }, 3);
      return;
    }
    rest.post({
      methodId: '5018',
      data: $scope.user,
      success: function(res) {
        if (res.code == 0) {
          $localStorage.user.isTransPasswordSet = 1;
          $scope.isSetPassword = 1;
          tipService.show("body", {
            message: "密码创建成功"
          }, 3);
        } else {
          tipService.show("body", {
            message: "密码创建失败!",
            notice: "danger"
          }, 3);
        }
      },
      error: function() {

      }
    })
  }


});